<?php

require_once(WSB_MODULES_PATH. 'cms/models/DbTable/Container.php');
require_once(WSB_XTENSION_PATH . 'flex/models/Table.php');

class Xtflex_IndexController extends WSB_Controller_Action
{

    private static $_container;

    protected $_isFrontendExtension = true;

	public function init()
	{
		parent::init(); // immer zuerst!
	    $this->_models = array('Flex_Model_Table');
	}

	public function indexAction()
	{
        $this->listAction();
	}

	public function flexAction()
	{

        $flexDefinitionRow = $this->_getFlexDefinitionRow();
        $this->view->flexDefinitionRow = $flexDefinitionRow;

        $values = $this->_getFlexValues();

        if(!empty($_POST['save']) || !empty($_POST['saveAndClose'])) {
            $container = $this->_getContainer();
            $values = $this->_getFilteredPostValues();
            $container->flex = serialize($values);
            $container->save();
        }

        if(!empty($_POST['cancel']) || !empty($_POST['saveAndClose'])) {
            if($this->_getParam('TEMPLATE') == 'popup') {
                $this->_response->appendBody('
                <script type="text/javascript">
                    window.opener.location.reload();
                    window.close();
                </script>
                ');
            } else {
                $container = $this->_getContainer();
                $this->_helper->redirector('edit', 'container', 'cms', array('page_id' => $container->page_id));
            }
        }

        $this->view->assign($values);

        $this->render('flex');
		$this->render('container-tree', 'left', true);
	}

	private function _getFlexDefinitionRow()
	{
		$container = $this->_getContainer();

		$table = new Flex_Model_Table();
        $rowset = $table->find($container->item_id);
        if($rowset->count() >= 1) {
            return $rowset->current();
        }
        return null;
	}

	private function _getFilteredPostValues()
	{
        $values = array();
        foreach($_POST AS $key=>$value) {
            if(substr($key, 0, 1) != '_') {
                $values[$key] = trim($value);
            }
        }
        unset($values['submit']);
        return $values;
	}

	private function _getFlexValues()
	{
        $row = $this->_getContainer();
        $tmpValues = unserialize($row->flex);
        $values = array();
        if(is_array($tmpValues)) {
            foreach($tmpValues AS $key=>$value) {
                if(substr($key, 0, 1) != '_') {
                    $values[$key] = trim($value);
                }
            }
        }
        return $values;
	}

	private function _getContainer()
	{
	    if(empty($this->_container)) {
    	    $id = intval($this->_getParam('id'));
    	    $table = new Cms_Model_DbTable_Container();
            $rowset = $table->find($id);
            if($rowset->count() >= 1) {
                $row = $rowset->current();
                $this->_container = $row;
                return $this->_container;
            }
            return null;
	    } else {
	       return $this->_container;
	    }
	}

}
